/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.pd.projectbuilder.main.util;

import cz.pd.projectbuilder.beans.load.ProjectBean;
import cz.pd.projectbuilder.beans.load.ProjectBuilderBean;
import cz.pd.projectbuilder.beans.load.ProjectGroupBean;
import org.apache.commons.lang.SerializationUtils;

/**
 *
 * @author forrest
 */
public class BeanUtils {

  public static ProjectBuilderBean completeAllPathInProjctBean(ProjectBuilderBean projectBuilderBean) {
    ProjectBuilderBean cloneProjectBuilderBean = (ProjectBuilderBean) SerializationUtils.clone(projectBuilderBean);
    for (ProjectGroupBean projectGroupBean : cloneProjectBuilderBean.getProjectGroupBeans()) {
      for (ProjectBean projectBean : projectGroupBean.getProjectBeans()) {
        makeProjectBeanComplete(projectGroupBean, projectBean);
      }
    }
    return cloneProjectBuilderBean;
  }

  public static void makeProjectBeanComplete(ProjectGroupBean projectGroupBean, ProjectBean projectBean) {
    if (projectGroupBean.getSubprojekty() == null) {
      projectGroupBean.setSubprojekty(false);
    }
    if (projectBean.getSubprojekty() == null) {
      projectBean.setSubprojekty(projectGroupBean.getSubprojekty());
    }
    if (projectBean.getPath() == null) {
      projectBean.setPath(projectGroupBean.getGroupPathPreffix().concat(projectBean.getName()));
    }
    if (projectBean.getPathWithPom() == null) {
      StringBuilder sb = new StringBuilder(projectGroupBean.getGroupPathPreffix());
      sb.append("pom.xml");
      projectBean.setPathWithPom(sb.toString());
    }
  }

  public static ProjectBean generateNewSubProjectBean(ProjectBean projectBean, SubProjectType projectType) {
    ProjectBean cloneProjectBean = (ProjectBean) SerializationUtils.clone(projectBean);
    Assert.isNotNull(cloneProjectBean.getPath(), "ProjectBean musi obsahovat cestu!");

    StringBuilder sb = new StringBuilder(cloneProjectBean.getPath());
    sb.append("\\").append(cloneProjectBean.getName()).append("-").append(projectType.getText()).append("\\pom.xml");
    cloneProjectBean.setPathWithPom(sb.toString());
    cloneProjectBean.setName(cloneProjectBean.getName().concat("-" + projectType.getText()));
    return cloneProjectBean;
  }

  public enum SubProjectType {

    RCP("rcp"),
    SERVICE("service"),
    SERVICEIMPL("serviceimpl");
    private String text;

    private SubProjectType(String text) {
      this.text = text;
    }

    public String getText() {
      return text;
    }
  }
}
